using System;
using UnityEditor;
using UnityEngine;

namespace UnityXEditor
{
    [Serializable]
    public class ToolSettings
    {
        //------显示帧率(FPS)设置数据
        public enum SkipUnityLogoDelay
        {
            立即跳过 = 0,
            延迟10毫秒 = 10,
            延迟20毫秒 = 20,
            延迟50毫秒 = 50,
            延迟100毫秒 = 100,
            延迟200毫秒 = 200,
            延迟500毫秒 = 500,
            延迟1000毫秒 = 1000,
            延迟2000毫秒 = 2000,
            延迟3000毫秒 = 3000,
        }
        //------免费版Unity强制跳过发布的程序的启动画面(UnityLogo)设置数据
        [SerializeField]
        private bool m_EnabledSkipUnityLogo = false;
        [SerializeField]
        private SkipUnityLogoDelay m_SkipUnityLogoDelay = SkipUnityLogoDelay.立即跳过;
        public bool enabledSkipUnityLogo
        {
            get { return m_EnabledSkipUnityLogo; }
            set
            {
                if (value != m_EnabledSkipUnityLogo)
                {
                    m_EnabledSkipUnityLogo = value;
                    Update();
                }
            }
        }

        //------按Esc键退出程序设置数据
        [SerializeField]
        private bool m_EnabledEscExit = false;
        public bool enabledEscExit
        {
            get { return m_EnabledEscExit; }
            set
            {
                if (value != m_EnabledEscExit)
                {
                    m_EnabledEscExit = value;
                    Update();
                }
            }
        }

        //------显示帧率(FPS)设置数据
        public enum AnchorPoint
        {
            左上,
            左中,
            左下,
            中上,
            中中,
            中下,
            右上,
            右中,
            右下,
        }
        public enum FontSize
        {
            P6 = 6,
            P7 = 7,
            P8 = 8,
            P9 = 9,
            P10 = 10,
            P12 = 12,
            P14 = 14,
            P16 = 16,
            P18 = 18,
            P20 = 20,
            P24 = 24,
            P28 = 28,
            P32 = 32,
            P36 = 36,
            P40 = 40,
            P48 = 48,
            P56 = 56,
            P64 = 64,
            P72 = 72,
            P80 = 80,
        }
        [SerializeField]
        private bool m_EnabledFPSShow = false;
        [SerializeField]
        private AnchorPoint m_AnchorPoint = AnchorPoint.左上;
        [SerializeField]
        private Color m_FontColor = Color.green;
        [SerializeField]
        private FontSize m_FontSize = FontSize.P16;
        public bool enabledFPSShow
        {
            get { return m_EnabledFPSShow; }
            set
            {
                if (value != m_EnabledFPSShow)
                {
                    m_EnabledFPSShow = value;
                    UpdateFPS();
                }
            }
        }

        //------设置帧率(FPS)设置数据
        public enum FrameRate
        {
            垂直同步 = -1,
            每秒5帧 = 5,
            每秒10帧 = 10,
            每秒15帧 = 15,
            每秒20帧 = 20,
            每秒24帧 = 24,
            每秒30帧 = 30,
            每秒40帧 = 40,
            每秒50帧 = 50,
            每秒60帧 = 60,
            每秒70帧 = 70,
            每秒80帧 = 80,
            每秒90帧 = 90,
            每秒100帧 = 100,
            每秒110帧 = 110,
            每秒120帧 = 120,
            每秒130帧 = 130,
            每秒140帧 = 140,
            每秒150帧 = 150,
            每秒160帧 = 160,
            每秒180帧 = 180,
            每秒200帧 = 200,
            每秒220帧 = 220,
            每秒240帧 = 240,
            每秒260帧 = 260,
            每秒280帧 = 280,
            每秒300帧 = 300,
            每秒320帧 = 320,
            每秒340帧 = 340,
            每秒360帧 = 360,
            无限制 = 1000,
        }
        [SerializeField]
        private bool m_EnabledFPSSetting = false;
        [SerializeField]
        private FrameRate m_FrameRate = FrameRate.每秒120帧;
        public bool enabledFPSSetting
        {
            get { return m_EnabledFPSSetting; }
            set
            {
                if (value != m_EnabledFPSSetting)
                {
                    m_EnabledFPSSetting = value;
                    UpdateFPS();
                }
            }
        }

        //------暴露Unity内部循环系统(UnityLoop)设置数据
        [SerializeField]
        private bool m_EnabledUnityLoop = false;
        public bool enabledUnityLoop
        {
            get { return m_EnabledUnityLoop; }
            set
            {
                if (value != m_EnabledUnityLoop)
                {
                    m_EnabledUnityLoop = value;
                    Update();
                }
            }
        }

        //自动设置版本号
        public AutosetVersionNumberSetting autosetVersionNumber { get { return m_AutosetVersionNumber; } }
        [SerializeField]
        private AutosetVersionNumberSetting m_AutosetVersionNumber = new AutosetVersionNumberSetting();

        public void OnGUI()
        {
            EditorGUILayout.LabelField("工具", EditorStyles.boldLabel);
            EditorGUI.indentLevel++;
            {
                //------免费版Unity强制跳过发布的程序的启动画面(UnityLogo)设置数据
                EditorGUILayout.BeginHorizontal();
                m_EnabledSkipUnityLogo = EditorGUILayout.Toggle("免费版Unity强制跳过发布的程序的启动画面(UnityLogo)", m_EnabledSkipUnityLogo);
                EditorGUI.BeginDisabledGroup(!m_EnabledSkipUnityLogo);
                m_SkipUnityLogoDelay = (SkipUnityLogoDelay)EditorGUILayout.EnumPopup(m_SkipUnityLogoDelay);
                EditorGUI.EndDisabledGroup();
                EditorGUILayout.EndHorizontal();

                //------按Esc键退出程序设置数据
                m_EnabledEscExit = EditorGUILayout.Toggle("按Esc键退出程序", m_EnabledEscExit);

                //------显示帧率(FPS)设置数据
                EditorGUILayout.BeginHorizontal();
                m_EnabledFPSShow = EditorGUILayout.Toggle("显示帧率(FPS)", m_EnabledFPSShow);
                EditorGUI.BeginDisabledGroup(!m_EnabledFPSShow);
                m_AnchorPoint = (AnchorPoint)EditorGUILayout.EnumPopup(m_AnchorPoint);
                if (m_EnabledFPSShow)
                    m_FontColor = EditorGUILayout.ColorField(m_FontColor);
                else
                    EditorGUILayout.ColorField(new Color(m_FontColor.r * 0.5f, m_FontColor.g * 0.5f, m_FontColor.b * 0.5f, 0));
                EditorGUI.EndDisabledGroup();
                EditorGUILayout.EndHorizontal();

                //------设置帧率(FPS)设置数据
                EditorGUILayout.BeginHorizontal();
                m_EnabledFPSSetting = EditorGUILayout.Toggle("设置帧率(FPS)", m_EnabledFPSSetting);
                EditorGUI.BeginDisabledGroup(!m_EnabledFPSSetting);
                m_FrameRate = (FrameRate)EditorGUILayout.EnumPopup(m_FrameRate);
                EditorGUI.EndDisabledGroup();
                EditorGUILayout.EndHorizontal();

                //------暴露Unity内部循环系统(UnityLoop)设置数据
                m_EnabledUnityLoop = EditorGUILayout.Toggle("暴露Unity内核底层循环系统(UnityLoop)", m_EnabledUnityLoop);

                m_AutosetVersionNumber.OnGUI();
            }
            EditorGUI.indentLevel--;
        }
        public void Update()
        {
            m_AutosetVersionNumber.Update();
            UpdateSkipUnityLogo();
            UpdateFPS();
            CodeFileTool.CommentsLine("UnityLoop.cs", "#define ENABLED", m_EnabledUnityLoop);
            CodeFileTool.CommentsLine("EscExit.cs", "#define ENABLED", m_EnabledEscExit);
        }
        private void UpdateSkipUnityLogo()
        {
            CodeFileTool.CommentsLine("SkipUnityLogo.cs", "#define ENABLED", m_EnabledSkipUnityLogo);
            if (m_EnabledSkipUnityLogo)
            {
                switch (m_SkipUnityLogoDelay)
                {
                    case SkipUnityLogoDelay.立即跳过:
                        CodeFileTool.CommentsLine("SkipUnityLogo.cs", "System.Threading.Thread.Sleep(", ";", false);
                        break;
                    default:
                        CodeFileTool.CommentsLine("SkipUnityLogo.cs", "System.Threading.Thread.Sleep(", ";", true);
                        CodeFileTool.ReplaceLine("SkipUnityLogo.cs", "System.Threading.Thread.Sleep(", ";", $"System.Threading.Thread.Sleep({(int)m_SkipUnityLogoDelay});");
                        break;
                }
            }
        }
        private void UpdateFPS()
        {
            CodeFileTool.CommentsLine("FPSSetting.cs", "#define ENABLED", m_EnabledFPSSetting);
            if (m_EnabledFPSSetting)
            {
                switch (m_FrameRate)
                {
                    case FrameRate.无限制:
                        CodeFileTool.ReplaceLine("FPSSetting.cs", "QualitySettings.vSyncCount", ";", $"QualitySettings.vSyncCount = 0;");
                        CodeFileTool.ReplaceLine("FPSSetting.cs", "Application.targetFrameRate", ";", $"Application.targetFrameRate = -1;");
                        break;
                    case FrameRate.垂直同步:
                        CodeFileTool.ReplaceLine("FPSSetting.cs", "QualitySettings.vSyncCount", ";", $"QualitySettings.vSyncCount = 1;");
                        CodeFileTool.ReplaceLine("FPSSetting.cs", "Application.targetFrameRate", ";", $"Application.targetFrameRate = -1;");
                        break;
                    default:
                        CodeFileTool.ReplaceLine("FPSSetting.cs", "QualitySettings.vSyncCount", ";", $"QualitySettings.vSyncCount = 0;");
                        CodeFileTool.ReplaceLine("FPSSetting.cs", "Application.targetFrameRate", ";", $"Application.targetFrameRate = {(int)m_FrameRate};");
                        break;
                }
            }

            CodeFileTool.CommentsLine("FPSShow.cs", "#define ENABLED", m_EnabledFPSShow);
            if (m_EnabledFPSShow)
            {
                switch (m_AnchorPoint)
                {
                    case AnchorPoint.左上:
                        CodeFileTool.ReplaceLine("FPSShow.cs", "public Vector2 anchorPoint", ";", $"public Vector2 anchorPoint = new Vector2(0, 1);");
                        break;
                    case AnchorPoint.左中:
                        CodeFileTool.ReplaceLine("FPSShow.cs", "public Vector2 anchorPoint", ";", $"public Vector2 anchorPoint = new Vector2(0, 0.5f);");
                        break;
                    case AnchorPoint.左下:
                        CodeFileTool.ReplaceLine("FPSShow.cs", "public Vector2 anchorPoint", ";", $"public Vector2 anchorPoint = new Vector2(0, 0);");
                        break;
                    case AnchorPoint.中上:
                        CodeFileTool.ReplaceLine("FPSShow.cs", "public Vector2 anchorPoint", ";", $"public Vector2 anchorPoint = new Vector2(0.5f, 1);");
                        break;
                    case AnchorPoint.中中:
                        CodeFileTool.ReplaceLine("FPSShow.cs", "public Vector2 anchorPoint", ";", $"public Vector2 anchorPoint = new Vector2(0.5f, 0.5f);");
                        break;
                    case AnchorPoint.中下:
                        CodeFileTool.ReplaceLine("FPSShow.cs", "public Vector2 anchorPoint", ";", $"public Vector2 anchorPoint = new Vector2(0.5f, 0);");
                        break;
                    case AnchorPoint.右上:
                        CodeFileTool.ReplaceLine("FPSShow.cs", "public Vector2 anchorPoint", ";", $"public Vector2 anchorPoint = new Vector2(1, 1);");
                        break;
                    case AnchorPoint.右中:
                        CodeFileTool.ReplaceLine("FPSShow.cs", "public Vector2 anchorPoint", ";", $"public Vector2 anchorPoint = new Vector2(1, 0.5f);");
                        break;
                    case AnchorPoint.右下:
                        CodeFileTool.ReplaceLine("FPSShow.cs", "public Vector2 anchorPoint", ";", $"public Vector2 anchorPoint = new Vector2(1, 0);");
                        break;
                }
                CodeFileTool.ReplaceLine("FPSShow.cs", "public Color fontColor", ";", $"public Color fontColor = new Color({m_FontColor.r}f, {m_FontColor.g}f, {m_FontColor.b}f);");
                CodeFileTool.ReplaceLine("FPSShow.cs", "public int fontSize", ";", $"public int fontSize = {(int)m_FontSize};");
            }
        }
    }
}